# author 韩佳乐
# 开发时间 2025/6/24 10:01
from django import forms
from django.core.exceptions import ValidationError
import re

from django.shortcuts import render

from app5.models import UserBaseInfo


def mobile_validate(value):
    mobile_re = re.compile(
        r'^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$')
    if not mobile_re.match(value):
        raise ValidationError('手机号码格式错误')


def age_validate(value):
    if value < 1 or value > 120:
        raise ValidationError('年龄范围为1-120岁')

class UserInfoForm(forms.Form):
    # 用户状态
    STATUS = ((None,'请选择'),(0,'正常'),(1,'无效'))
    username = forms.CharField(label="用户名称",min_length=6,max_length=30,widget=forms.widgets.TextInput(attrs={'class':'form-control','placeholder':'请输入用户名称'}))
    password = forms.CharField(label='密码',min_length=6,max_length=10,help_text='全部为数字',widget=forms.widgets.PasswordInput(attrs={'class':'password'},render_value=True))
    age = forms.IntegerField(label="年龄",initial=18)
    mobile = forms.CharField(label="手机号码")
    status = forms.ChoiceField(label="用户状态",choices=STATUS)
    createdate = forms.DateTimeField(label="创建时间",required=False,input_formats=['%Y-%m-%d %H:%M:%S'])
# 模型表单
class UserInfoModelForm(forms.ModelForm):
    confirm_password = forms.CharField(
        label='确认密码', widget=forms.PasswordInput(render_value=True),
        error_messages={
            'required': '密码不能为空',
        })
    class Meta:
        # 定义关联模型
        model = UserBaseInfo
        # 定义需要再表单中展示的字段
        fields = ('username','password','confirm_password','age','mobile','status')
        # 如果需要展示全部字段，则可以如下设置
        #fields="__all__"
        # 如果在Models中定义了名称，则在这里不用再定义
        labels = {"age":"年龄","mobile":"手机信息"}
        # 将文本框渲染为密码输入框
        widgets = {"password":forms.widgets.PasswordInput(attrs={'class':'password'},render_value=True)}
        error_messages = {
            "username": {
                'required': '用户姓名不能为空',
                'min_length': '长度最少6位',
                'invalid': '输入正确的用户姓名'
            },
            "password": {
                'max_length': '密码最长10位',
                'required': '密码不能为空',
                'min_length': '密码最少6位'
            },
            "age": {
                'required': '年龄不能为空',
            },
            "mobile": {
                'required': '手机号码不能为空',
            },
            "status": {
                'required': '用户状态不能为空',
            }
        }

    # 校验手机号的局部钩子函数
    def clean_mobile(self):
        mobile = self.cleaned_data.get('mobile')
        mobile_re = re.compile(r'^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$')
        if not mobile_re.match(mobile):
            raise ValidationError('手机号码格式错误')
        return mobile

    # 全局钩子函数
    def clean(self):
        print('执行全局钩子函数')
        password = self.cleaned_data.get('password')
        confirm_password = self.cleaned_data.get("confirm_password")
        if password != confirm_password:
            raise forms.ValidationError("两次密码输入不一致")
        return self.cleaned_data

class UserBaseInfoForm(forms.Form):
    # 用户状态
    STATUS = ((None, '请选择'), (0, '正常'), (1, '无效'))
    username = forms.CharField(label = "用户名称",min_length=3,error_messages={'required':'用户名称不可以为空！1','min_length':'长度最少为3位','invalid':"请输入正确的用户姓名"})
    age = forms.IntegerField(label= "年龄",initial=18,validators=[age_validate],error_messages={'required':'年龄不可以为空'})
    status = forms.ChoiceField(label='用户状态',choices=STATUS,error_messages={'required':"用户状态不能为空"})
    createdate = forms.DateTimeField(label='创建时间',required=False,input_formats=['%Y-%m-%d %H:%M:%S'])

class UserInfo_Msg_Form(forms.Form):
    '''用户状态'''
    STATUS = ((None, '请选择'), (0, '正常'), (1, '无效'),)
    username = forms.CharField(label="用户名", min_length=6,
                               widget=forms.widgets.TextInput(
                                   attrs={'class': 'form-control', 'placeholder': "请输入用户名"}),
                               error_messages={
                                   'required': '用户姓名不能为空',
                                   'min_length': '长度最少6位',
                                   'invalid': '输入正确的用户姓名'
                               })
    password = forms.CharField(label="密码", min_length=6, max_length=10,
                               widget=forms.widgets.PasswordInput(
                                   attrs={"class": "password"}, render_value=True),
                               error_messages={
                                   'max_length': '密码最长10位',
                                   'required': '密码不能为空',
                                   'min_length': '密码最少6位'
                               })
    age = forms.IntegerField(label="年龄", initial=18, validators=[age_validate],
                             error_messages={
                                    'required': '年龄不能为空',
    })
    mobile = forms.CharField(label="手机号码", validators=[mobile_validate],
                             error_messages={
                                    'required': '手机号码不能为空',
    })
    status = forms.ChoiceField(label="用户状态", choices=STATUS,
                               error_messages={
                                   'required': '用户状态不能为空',
                               })
    createdate = forms.DateTimeField(label="创建时间", required=False)

class ImgFileForm(forms.Form):
    name = forms.CharField()
    headimg = forms.FileField()